﻿#include "ssFrameWnd.h"


namespace StarSeeker
{

ssWin32Message* ssFrameWnd::s_pMSG = 0;
bool			ssFrameWnd::s_windowClose = true;

//消息回调函数
static LRESULT CALLBACK FrameProc(HWND hwnd, //窗口句柄
	UINT uMsg, //消息类型
	WPARAM wParam, //消息的w参数
	LPARAM lParam // 消息的l参数
	)
{
	if (uMsg == WM_CLOSE)
	{
		if (ssFrameWnd::s_pMSG)
			ssFrameWnd::s_pMSG->RouteMessage(uMsg, wParam, lParam);
		DestroyWindow(hwnd);
		return 0;
	}
	else if (uMsg == WM_DESTROY)
	{
		ssFrameWnd::s_windowClose = true;
		PostQuitMessage(0);
		return 0;
	}
	else if (ssFrameWnd::s_pMSG)
	{
		if (ssFrameWnd::s_pMSG->RouteMessage(uMsg, wParam, lParam))
			return 0;
		else
			return DefWindowProc(hwnd, uMsg, wParam, lParam);
	}
	else
	{
		return DefWindowProc(hwnd, uMsg, wParam, lParam);
	}
}

ssFrameWnd::ssFrameWnd(void)
:m_hWnd(0),m_hInstance(0),m_hDC(0)
{
	memset(&m_wc,0,sizeof(WNDCLASSW));
	m_nWidth = 1024;
	m_nHeight = 768;
}

ssFrameWnd::~ssFrameWnd(void)
{
}

bool ssFrameWnd::BuildWindow( HINSTANCE hInstance, LPCWSTR pClassName )
{
	m_hInstance = hInstance;
	m_wc.lpszClassName = pClassName;
	m_wc.lpfnWndProc = FrameProc;
	m_wc.hCursor = LoadCursor(0, IDC_HELP);
	m_wc.style = CS_HREDRAW | CS_VREDRAW;
	//m_wc.hIcon = LoadIconW(hInstance, L"IDI_ICON1");
	::RegisterClassW(&m_wc);
	m_hWnd = ::CreateWindowW(m_wc.lpszClassName, m_wc.lpszClassName, /*WS_EX_TOPMOST*/WS_OVERLAPPEDWINDOW,
							0, 0, m_nWidth, m_nHeight, 0, 0, hInstance, 0);
	if (!m_hWnd) return false;

	m_hDC = ::GetDC(m_hWnd);
	if (!m_hDC) return false;

	::ShowWindow(m_hWnd, SW_SHOWNORMAL);
	::UpdateWindow(m_hWnd);
	ssFrameWnd::s_windowClose = false;
	return true;
}

void ssFrameWnd::PeekMessage()
{
	static MSG msg;
	if(::PeekMessage(&msg, m_hWnd, 0, 0, PM_REMOVE))
	{
		::TranslateMessage(&msg);
		::DispatchMessage(&msg);
	}
}

}
